//
//  MessageRemind.m
//  Messenger
//
//  Created by OC on 15/6/8.
//  Copyright (c) 2015年 YN-APP-iOS. All rights reserved.
//

#import "RemindSet.h"
#import "YNIMFrameworkMacro.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"
#import "YNIMTool.h"

@implementation RemindSet

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if (!value) {
        if ([key isEqualToString:@"isVibrate"]) {
            self.isVibrate=0;
        }else if ([key isEqualToString:@"isGroupVibrate"]){
            self.isGroupVibrate=0;
        }else if ([key isEqualToString:@"isAudio"]){
            self.isAudio=0;
        }else if ([key isEqualToString:@"isGroupAudio"]){
            self.isGroupAudio=0;
        }else if ([key isEqualToString:@"isDistractionFree"]){
            self.isDistractionFree=0;
        }
    }
}

+ (NSString *)StartTime
{
   return @"DistractionFree_startTime";
}

+ (NSString *)EndTime
{
   return @"DistractionFree_endTime";
}

+ (NSString *)Vibrate
{
    return @"isVibrate";
}

+ (NSString *)GroupVibrate
{
    return @"isGroupVibrate";
}

+ (NSString *)Audio
{
    return @"isAudio";
}

+ (NSString *)GroupAudio
{
    return @"isGroupAudio";
}

+ (NSString *)DistractionFree
{
    return @"isDistractionFree";
}

+ (NSString *)TableName
{
    return @"RemindSet";
}

- (NSArray *)insertParamOfRemind
{
    return @[[NSNumber numberWithInteger:self.isDistractionFree],
             [YNIMTool nilFilter:self.DistractionFree_startTime],
             [YNIMTool nilFilter:self.DistractionFree_endTime],
             [NSNumber numberWithInteger:self.isAudio],
             [NSNumber numberWithInteger:self.isGroupAudio],
             [NSNumber numberWithInteger:self.isVibrate],
             [NSNumber numberWithInteger:self.isGroupVibrate]
             ];
}

//创建消息提醒表
+ (BOOL)createInDatabase:(FMDatabase *)db
{
    if ([db tableExists:[RemindSet TableName]]) {
        return YES;
    }
    NSString *createMessageRemindSql = [NSString stringWithFormat:@"%@%@(%@ INTEGER, %@ varchar(36), %@ varchar(36),%@ INTEGER,%@ INTEGER,%@ INTEGER,%@ INTEGER)",CREATE_TABLE,[RemindSet TableName],
                                        [RemindSet DistractionFree],
                                        [RemindSet StartTime],
                                        [RemindSet EndTime],
                                        [RemindSet Audio],
                                        [RemindSet GroupAudio],
                                        [RemindSet Vibrate],
                                        [RemindSet GroupVibrate]];

    BOOL result = [db executeUpdate:createMessageRemindSql];
    return result;
}

//插入消息提醒设置表
- (BOOL)insertInDatabase:(FMDatabase *)db
{
    NSString *fieldString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@",
                             [RemindSet DistractionFree],
                             [RemindSet StartTime],
                             [RemindSet EndTime],
                             [RemindSet Audio],
                             [RemindSet GroupAudio],
                             [RemindSet Vibrate],
                             [RemindSet GroupVibrate]];
    
    NSString *insertSql = [NSString stringWithFormat:@"%@%@ (%@) VALUES (?,?,?,?,?,?,?)",INSERT_TABLE,[RemindSet TableName],fieldString];
    BOOL result = [db executeUpdate:insertSql withArgumentsInArray:[self insertParamOfRemind]];
    return result;
}

//更新消息提醒设置表
- (BOOL)updateInDatabase:(FMDatabase *)db
{
    NSString *updateSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?, %@ = ?",[RemindSet TableName],
                                [RemindSet DistractionFree],
                                [RemindSet StartTime],
                                [RemindSet EndTime],
                                [RemindSet Audio],
                                [RemindSet GroupAudio],
                                [RemindSet Vibrate],
                                [RemindSet GroupVibrate]];
    
    BOOL result = [db executeUpdate:updateSql withArgumentsInArray:[self insertParamOfRemind]];
    return result;
}

//查询消息提醒设置信息
+ (NSArray *)queryInDatabase:(FMDatabase *)db
{
    NSString *querySql = [NSString stringWithFormat:@"SELECT *FROM %@",[RemindSet TableName]];
    FMResultSet *rs = [db executeQuery:querySql];
    NSMutableArray *remindArray = [NSMutableArray array];
    while ([rs next])
    {
        RemindSet *remindSet = [[RemindSet alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [remindSet setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        [remindArray addObject:remindSet];
    }
    
    [rs close];
    
    return remindArray;
}



@end
